// 测试晶体结构API端点
const http = require('http');

async function testCrystalAPI() {
    console.log('测试晶体结构API...');
    
    function fetchAPI(url) {
        return new Promise((resolve, reject) => {
            http.get(url, (response) => {
                let data = '';
                response.on('data', (chunk) => {
                    data += chunk;
                });
                response.on('end', () => {
                    try {
                        resolve(JSON.parse(data));
                    } catch (error) {
                        reject(error);
                    }
                });
            }).on('error', (error) => {
                reject(error);
            });
        });
    }
    
    try {
        // 测试健康检查
        console.log('1. 测试健康检查...');
        const healthData = await fetchAPI('http://127.0.0.1:8001/health');
        console.log('健康检查结果:', healthData);
        
        // 测试获取硅的晶体结构
        console.log('2. 测试获取硅的晶体结构...');
        const siliconData = await fetchAPI('http://127.0.0.1:8001/materials/mp-149/structure');
        console.log('硅晶体结构数据:');
        console.log('- 材料ID:', siliconData.material_id);
        console.log('- 晶胞参数: a=', siliconData.lattice.a, 'b=', siliconData.lattice.b, 'c=', siliconData.lattice.c);
        console.log('- 原子数量:', siliconData.sites.length);
        console.log('- 原子位置示例:', siliconData.sites[0]);
        
        // 测试获取金刚石结构
        console.log('3. 测试获取金刚石结构...');
        const diamondData = await fetchAPI('http://127.0.0.1:8001/materials/mp-66/structure');
        console.log('金刚石结构数据:');
        console.log('- 材料ID:', diamondData.material_id);
        console.log('- 晶胞参数: a=', diamondData.lattice.a, 'b=', diamondData.lattice.b, 'c=', diamondData.lattice.c);
        console.log('- 原子数量:', diamondData.sites.length);
        
        // 测试获取石英结构
        console.log('4. 测试获取石英结构...');
        const quartzData = await fetchAPI('http://127.0.0.1:8001/materials/mp-1840/structure');
        console.log('石英结构数据:');
        console.log('- 材料ID:', quartzData.material_id);
        console.log('- 晶胞参数: a=', quartzData.lattice.a, 'b=', quartzData.lattice.b, 'c=', quartzData.lattice.c);
        console.log('- 原子数量:', quartzData.sites.length);
        
        console.log('API测试完成！所有端点正常工作。');
        
    } catch (error) {
        console.error('API测试失败:', error);
        console.log('请确保后端服务正在运行在 http://localhost:8001');
    }
}

// 运行测试
testCrystalAPI();